package com.gyy.faq.controller;

import com.gyy.faq.pojo.dto.FaqBackFeedDTO;
import com.gyy.faq.pojo.dto.FaqClassifyDTO;
import com.gyy.faq.pojo.dto.FaqProblemDTO;
import com.gyy.faq.service.FaqService;
import com.gyy.faq.validator.group.FaqGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Lowi on 2018/6/25 0025.
 */
@Controller
@RequestMapping(value = "faq")
public class FaqController {
	@Autowired
	private FaqService faqService;

	@RequestMapping(value = "/select/hotProblem")
	@ResponseBody
	public Result selectHotProblem() {
		Result result = faqService.selectHotProblem();
		return result;
	}

	@RequestMapping(value = "/select/problemInfo")
	@ResponseBody
	public Result selectProblemInfo(@Validated({FaqGroup.SelectFaqProblemInfo.class}) FaqProblemDTO faqProblemDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.selectProblemInfo(faqProblemDTO);
		return result;
	}

	@RequestMapping(value = "/homepage/classifyList")
	@ResponseBody
	public Result homepageClassifyList() {
		Result result = faqService.homepageClassifyList();
		return result;
	}

	@RequestMapping(value = "/select/problemList")
	@ResponseBody
	public Result selectProblemList(@Validated FaqProblemDTO faqProblemDTO) {
		faqProblemDTO.setOffset((faqProblemDTO.getPage() - 1) * faqProblemDTO.getCount());
		Result result = faqService.selectProblemList(faqProblemDTO);
		return result;
	}

	@RequestMapping(value = "/insert/parentClassify")
	@ResponseBody
	public Result insertParentClassify(@Validated({FaqGroup.InsertClassify.class}) FaqClassifyDTO faqClassifyDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.insertParentClassify(faqClassifyDTO);
		return result;
	}

	@RequestMapping(value = "/insert/sonClassify")
	@ResponseBody
	public Result insertSonClassify(@Validated({FaqGroup.InsertSonClassify.class}) FaqClassifyDTO faqClassifyDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.insertSonClassify(faqClassifyDTO);
		return result;
	}

	@RequestMapping(value = "/delete/classify")
	@ResponseBody
	public Result deleteClassify(@Validated({FaqGroup.DeleteClassify.class}) FaqClassifyDTO faqClassifyDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.deleteClassify(faqClassifyDTO);
		return result;
	}

	@RequestMapping(value = "/insert/problem")
	@ResponseBody
	public Result insertProblem(@Validated({FaqGroup.InsertProblem.class}) FaqProblemDTO faqProblemDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.insertProblem(faqProblemDTO);
		return result;
	}

	@RequestMapping(value = "/select/classifyList")
	@ResponseBody
	public Result selectClassifyList(@Validated FaqClassifyDTO faqClassifyDTO) {
		Result result = faqService.selectClassifyList(faqClassifyDTO);
		return result;
	}

	@RequestMapping(value = "/edit/problem")
	@ResponseBody
	public Result editProblem(@Validated({FaqGroup.EditProblem.class}) FaqProblemDTO faqProblemDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.editProblem(faqProblemDTO);
		return result;
	}

	@RequestMapping(value = "/delete/problem")
	@ResponseBody
	public Result deleteProblem(@Validated({FaqGroup.DeleteProblem.class}) FaqProblemDTO faqProblemDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.deleteProblem(faqProblemDTO);
		return result;
	}

	@RequestMapping(value = "/edit/problemClassify")
	@ResponseBody
	public Result editProblemClassify(@Validated({FaqGroup.EditProblemClassify.class}) FaqProblemDTO faqProblemDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.editProblemClassify(faqProblemDTO);
		return result;
	}

	@RequestMapping(value = "/submit/backFeed")
	@ResponseBody
	public Result submitBackFeed(@Validated({FaqGroup.InsertProblemBackFeed.class}) FaqBackFeedDTO faqBackFeedDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.submitBackFeed(faqBackFeedDTO);
		return result;
	}

	@RequestMapping(value = "/update/backFeed")
	@ResponseBody
	public Result updateBackFeed(@Validated({FaqGroup.UpdateProblemBackFeed.class}) FaqBackFeedDTO faqBackFeedDTO, Errors errors) {
		if (errors.hasErrors()) {
			return new Result(errors);
		}
		Result result = faqService.updateBackFeed(faqBackFeedDTO);

		return result;
	}

	@RequestMapping(value = "/redisTest")
	@ResponseBody
	private void redisTest() {
		faqService.redisTest();
	}
}
